package com.sskj.run.ui.activity;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.run.R;
import com.sskj.run.R2;
import com.sskj.run.bean.OpenAwardBean;
import com.sskj.run.component.DaggerUserDataComponent;
import com.sskj.run.presenter.EveryStageFunctionActivityPresenter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

@Route(path = RConfig.RUN_EVERY_STAGE_FUNCTION)//每期开奖
public class EveryStageFunctionActivity extends BaseActivity<EveryStageFunctionActivityPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    @Inject
    UserViewModel userViewModel;
    private SlimAdapter slimAdapter;
    private MVCCoolHelper<List<OpenAwardBean>> mvcHelper;
    private UserData userData;

    @Override
    protected int getLayoutId() {
        return R.layout.run_activity_every_stage_function;
    }

    @Override
    public EveryStageFunctionActivityPresenter getPresenter() {
        return new EveryStageFunctionActivityPresenter();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mvcHelper != null) {
            mvcHelper.refresh();
        }
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.run_everyStageFunctionActivity1));
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapter = SlimAdapter.create().register(R.layout.run_recy_item_every_stage_function, new SlimInjector<OpenAwardBean>() {
            @Override
            public void onInject(OpenAwardBean data, IViewInjector injector, List payloads) {
                if (slimAdapter.getData().indexOf(data) % 2 == 0) {
                    injector.backgroundColor(R.id.llBg, ContextCompat.getColor(App.INSTANCE, R.color.app_7d7));
                } else {
                    injector.backgroundColor(R.id.llBg, ContextCompat.getColor(App.INSTANCE, R.color.app_696));
                }
                injector.text(R.id.tv_periodID, data.getPeriodID())
                        .text(R.id.tv_boomValue, data.getBoomValue())
                        .text(R.id.tv_boomHash, data.getBoomHash())
                        .clicked(R.id.tv_check, view -> {
                            ARouter.getInstance().build(RConfig.RUN_CHECKOUT).withString(Constans.BOOMHASH, data.getBoomHash()).navigation();
                        });
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
//        ModelRx2DataSource<OpenAwardBean> dataSource = new ModelRx2DataSource<>(
//                (ModelRx2DataSource.OnLoadSource<OpenAwardBean>)
//                        page -> mPresenter.getOpenAwardList( page, 10), 10);
//        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
//        mvcHelper.setDataSource(dataSource);
//        mvcHelper.setAdapter(slimAdapter);
//        mvcHelper.refresh();
    }

}
